package com.weitu.permission.role.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.base.entity.Tree;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.role.entity.Role;
import com.weitu.permission.role.service.RoleService;
import com.weitu.permission.roleGroup.entity.RoleGroup;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.IdGenerator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sun.rmi.rmic.iiop.IDLGenerator;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by smp on 2017/3/17.
 */
@Controller
@RequestMapping(value = "permission/role")
public class RoleController extends BaseController {
    private static final String MODULE_BATH_PATH=BASE_JSP_PATH+"permission/role";
    private final RoleService roleService;

    @Autowired
    public RoleController(RoleService roleService){
        this.roleService=roleService;
    }

    /**
     * 跳入角色列表页面
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return MODULE_BATH_PATH+"/list";
    }

    /**
     * 角色列表数据
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request){
        Map<String,Object> paramMap=new HashMap<String,Object>();
        putParametersIntoMap(paramMap);
        int count = this.roleService.queryCount(paramMap);
        DataGridPagination dataGridPagination=DataGridPagination.getInstance(request,count);

        PageView pageView=dataGridPagination.getPageView();

        paramMap.put("pageView",pageView);

        List<Role> dataList =this.roleService.queryList(paramMap);
        return dataGridPagination.flushDataGridData(dataList);
    }

    /**
     *弹出添加弹框
     * @return
     */
    @RequestMapping(value = "/addRole",method = RequestMethod.GET)
    public String addRole(){
        return MODULE_BATH_PATH+"/add";
    }

    /**
     * 角色添加
     * @param role
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addRole",method = RequestMethod.POST)
    @SystemLog(module = "角色管理",methods = "添加角色")
    public AjaxResult addRoleAjax(Role role){
        role.setId(IDGenerator.getId());
        role.setAddTime(DateTimeUtil.formatterDateTime());
        role.setAddUserId(BaseController.getSessionUser().getId());
        role.setDelState(0);
        int count=this.roleService.insertRole(role);
        AjaxResult result=new AjaxResult();
        if (count>0){
            result.setMessage("保存成功！");
            result.setSuccess(true);
            return result;
        }else {
            result.setMessage("保存失败！");
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 弹出编辑角色弹框
     * @param id
     * @return
     */
    @RequestMapping(value = "editRole",method = RequestMethod.GET)
    public ModelAndView editRole(String id){
        Role role =this.roleService.queryRoleById(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("role", role);
        modelAndView.setViewName(MODULE_BATH_PATH + "/edit");
        return modelAndView;
    }

    /**
     * 编辑角色
     * @param role
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "editRole",method = RequestMethod.POST)
    @SystemLog(module = "角色管理",methods = "编辑角色")
    public AjaxResult editRoleAjax(Role role){
        role.setUpdateTime(DateTimeUtil.formatterDateTime());
        role.setUpdateUserId(BaseController.getSessionUser().getId());
        int count=this.roleService.updateRole(role);
        AjaxResult result=new AjaxResult();
        if (count>0){
            result.setMessage("编辑成功！");
            result.setSuccess(true);
            return result;
        }else {
            result.setMessage("编辑失败！");
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteRole",method = RequestMethod.POST)
    @SystemLog(module = "角色管理",methods = "删除角色")
    public AjaxResult deleteRoleAjax(String id){
        int count = this.roleService.deleteRole(id);
        AjaxResult result=new AjaxResult();
        if (count>0){
            result.setMessage("删除成功！");
            result.setSuccess(true);
            return result;
        }else {
            result.setMessage("删除失败！");
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 删除角色组判断
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteRoleGroupJudgement",method = RequestMethod.POST)
    public AjaxResult deleteRoleGroupJudgement(HttpServletRequest request){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        int count=this.roleService.queryCount(paramMap);
        AjaxResult result = new AjaxResult();
        if (count>0){
            result.setMessage("该角色组下有角色不可以删除！");
            result.setSuccess(false);
            return result;
        }else {
            result.setMessage("该角色组下没有没有角色可以删除！");
            result.setSuccess(true);
            return result;
        }
    }
    /**
     * 角色和角色组树
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "roleAndRoleGroupTree",method = RequestMethod.POST)
    public List<Tree> roleAndRoleGroupTree(HttpServletRequest request){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        return this.roleService.roleAndRoleGroupTree(paramMap);
    }
    /**
     * 添加编辑判断code
     * @param code
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addAndAlterRoleJudgement",method = RequestMethod.POST)
    public AjaxResult addAndAlterRoleJudgement(String code){
        int count=this.roleService.queryCountByCode(code);
        AjaxResult result = new AjaxResult();
        if (count>0){
            result.setMessage("标识重复，保存失败！");
            result.setSuccess(false);
            return result;
        }else {
            result.setMessage("保存成功！");
            result.setSuccess(true);
            return result;
        }
    }
}
